<?php

namespace backend\controllers;

use common\helpers\CommonBack;
use common\helpers\commonApi;
use common\helpers\WechatTransfer;
use common\models\Campus;
use common\models\service\StatService;
use common\models\service\UserService;
use common\models\System;
use common\services\ConfigService;
use common\services\MenuService;

/**
 * 首页控制器
 * Class IndexController
 * @package backend\controllers
 */
class IndexController extends BaseController
{
    public function actionIndex()
    {
        $menuHtml = MenuService::getMenuListByLogin(1);
        $adminInfo = CommonBack::adminLoginInfo();
        unset($adminInfo['menu']);
        unset($adminInfo['role']);
        $docUrl = ConfigService::getConfig('sys_doc_url');
        $campus = Campus::getCampusAll(['delete' => 0], ['id', 'name']);
        $logo = System::getModel()->logo;
        $mycampus = $this->campus_id;
        $mame = ConfigService::getConfig('sys_config_sysname');
        return $this->renderPartial('index', [
            'menuHtml' => $menuHtml,
            'adminInfo' => $adminInfo,
            'docUrl' => $docUrl,
            'campus' => $campus,
            'mycampus' => $mycampus,
            'logo' => $logo,
            'mame' => $mame,
        ]);
    }

    public function actionHome()
    {
//        WechatTransfer::transfer('a1123213213',0.01,'o5Kp65Hpl5j_z7vdasr6u6-dT1qs');
        $date = date('Y-m-d H:i:s');
        $data = StatService::statistics($this->campus_id);
        $record_no = ConfigService::getConfig('sys_config_record_no');
        $record_no_link = ConfigService::getConfig('sys_record_no_link');
        $copyright = ConfigService::getConfig('sys_copyright');
        return $this->render('',['date'=>$date,'data'=>$data,'record_no'=>$record_no,'record_no_link'=>$record_no_link,'copyright'=>$copyright]);
    }


    public function actionDownload()
    {
        $fileName = \Yii::$app->request->get('fileName', '');
        if (!$fileName) return $this->tError('参数错误');
        $root = \Yii::getAlias('@approot');
        if (!file_exists($root . $fileName)) return $this->tError('文件不存在');
        $fileUrl = commonApi::getDomain($fileName);
        header('location:' . $fileUrl);
    }

    public function actionSwitch($campus_id)
    {
        \Yii::$app->session->set('campus_id', $campus_id);
        return commonApi::message('切换成功');
    }

    /**
     * 营业额
     * @return array
     */
    public function actionTurnover()
    {
        $get = \Yii::$app->request->get();
        return StatService::getTurnover($get);
    }
}
